﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.administrator.sources;
using VQWEB.common;
using VQData;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for ContactHandler
    /// </summary>
    public class core_ContactHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Contact     
            string add_contact = context.Request["add_contact"];
            if (!string.IsNullOrEmpty(add_contact))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                var obj = bussiness.InsertOrUpdate<core_contact>(add_contact);
                context.Response.Write("Thực thi thành công !");            
            }
            string view_contact = context.Request["view_contact"];
            if (!string.IsNullOrEmpty(view_contact))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_contact = context.Request["list_contact"];
            if (!string.IsNullOrEmpty(list_contact))
            {
                RenderList(context);
            }
            string delete_contact = context.Request["delete_contact"];
            if (!string.IsNullOrEmpty(delete_contact))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.Delete<core_contact>(delete_contact);
                RenderList(context);
            }
            string lockunlock_contact = context.Request["lockunlock_contact"];
            if (!string.IsNullOrEmpty(lockunlock_contact))
            {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_contact>(lockunlock_contact, "contact_active");
                RenderList(context);
            }
            #endregion
        }

        private void RenderSingle(HttpContext context,int Id)
        {
            DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
            object obj = layer.Get<core_contact>(
               c => c.contact_id.Equals(Id),
               c => new
               {
                   id = c.contact_id,
                   address = c.contact_address,
                   fullname = c.contact_fullname,
                   phone = c.contact_phone,
                   email = c.contact_email,
                   content = c.contact_content,
                   date = c.contact_date,
                   active = c.contact_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
       private void RenderList(HttpContext context) {

             DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
             IList<object> list = layer.List<core_contact>(
                 null,
                 null,
                 null,
                 null,
                 c => new
                 {
                     id = c.contact_id,
                     address = c.contact_address,
                     fullname = c.contact_fullname,
                     phone = c.contact_phone,
                     email = c.contact_email,
                     content = c.contact_content,
                     date = c.contact_date,
                     active = c.contact_active
                 });
             string json = serializer.Serialize(list);
             context.Request.ContentType = "aplication/json";
             context.Response.Write(json);
            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}